//这个文件专门用来存放关于首页的所有接口
const Router = require('koa-router')
const home = new Router()
const {exec} = require('../util/db')
// const db = require('../util/db')
const bodyparser = require('koa-bodyparser')
home.use(bodyparser())//调用这个中间件 就可以拿到前端post过来的数据


//写对应的接口
// get请求
// http://localhost:3000/home/drug获取所有的药品
home.get('/drug', async (ctx) => {
    let selectSql = `select * from drug_information`
    let res = await exec(selectSql)
    ctx.body = res
})

// http://localhost:3000/home/drug/list?drugName=''或者?drugClassification=''或者?drugBrand=''
home.get('/drug/list', async (ctx) => {
    console.log('-----------')
    let obj = ctx.query
    if(obj.drugName){
        let selectSql = `select * from drug_information where drugName='${obj.drugName}'`
        var res = await exec(selectSql)
    }else if(obj.drugClassification){
        let selectSql = `select * from drug_information where drugClassification='${obj.drugClassification}'`
        var res = await exec(selectSql)
    }else if(obj.drugBrand){
        let selectSql = `select * from drug_information where drugBrand='${obj.drugBrand}'`
        var res = await exec(selectSql)
    }else if(obj.drugId){
        let selectSql = `select * from drug_information where drugId='${obj.drugId}'`
        var res = await exec(selectSql)
    }
    for(var i=0;i<res.length;i++){
        res[i].drugImg=`/drugImg${res[i].drugImg}`
    }
    ctx.body = res
})

// post请求
// http://localhost:3000/home/drug
home.post('/drug', async (ctx) => {
    let obj = ctx.request.body
        let selectSql = `select * from drug_information`
        let users = await exec(selectSql)
        let flag = false
        // 判断数据库drug_information表中是否含有请求中的药品
        for (key in users) {
            if (users[key].drugId == obj.drugId) {
                flag = true
            }
        }
        // 如果没有，则进行插入
        if (flag == false) {
            let insertSql = `insert into drug_information(drugId,drugName,drugImg,drugClassification,
                drugBrand,producer,indication,usageMethod,pharmacologicalEffect,
                mattersNeedingAttention,composition) 
                values('${obj.drugId}','${obj.drugName}','${obj.drugImg}','${obj.drugClassification}',
                '${obj.drugBrand}','${obj.producer}','${obj.indication}','${obj.usageMethod}',
                '${obj.pharmacologicalEffect}','${obj.mattersNeedingAttention}','${obj.composition}')`
            console.log(insertSql)

            await exec(insertSql)
            ctx.body = '插入成功啦'
        } else {//如果有，则进行更新
            let updateSql = `update drug_information 
            set 
            drugName='${obj.drugName}',
            drugImg='${obj.drugImg}' ,
            drugClassification='${obj.drugClassification}' ,
            drugBrand='${obj.drugBrand}' ,
            producer='${obj.producer}' ,
            indication='${obj.indication}' ,
            usageMethod='${obj.usageMethod}' ,
            pharmacologicalEffect='${obj.pharmacologicalEffect}' ,
            mattersNeedingAttention='${obj.mattersNeedingAttention}' ,
            composition='${obj.composition}' 
            where 
            drugId='${obj.drugId}'`
            await exec(updateSql)
            ctx.body = '更新成功啦'
        }
})

// delete请求
// http://localhost:3000/home/drug
home.delete('/drug', async (ctx) => {
    let obj = ctx.request.body
    let deleteSql = `delete from drug_information where drugId='${obj.drugId}'`
    await exec(deleteSql)
    ctx.body = '删除成功啦'
})


module.exports=home